<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" class="enhanced-dialog">
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:shopping-cart" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">{{ dialogTitle }}</h4>
        <p class="dialog-subtitle">购买套餐话费配置</p>
      </div>
    </div>

    <!-- 表单内容 -->
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="140px"
        class="enhanced-form"
      >
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:setting" class="section-icon" />
            <span>套餐配置</span>
          </div>

          <el-form-item label="分钟" prop="count" class="form-item">
            <el-input
              type="number"
              v-model="formData.count"
              placeholder="请输入购买分钟"
              class="enhanced-input"
            >
              <template #prefix>
                <Icon icon="ep:clock" />
              </template>
              <template #suffix>
                <span class="input-unit">分钟</span>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="有效期" prop="day" class="form-item">
            <el-input
              type="number"
              v-model="formData.day"
              placeholder="请输入有效期(天)"
              class="enhanced-input"
            >
              <template #prefix>
                <Icon icon="ep:calendar" />
              </template>
              <template #suffix>
                <span class="input-unit">天</span>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="指定员工" required class="form-item">
            <div class="employee-section">
              <div class="employee-header">
                <Icon icon="ep:user" class="employee-icon" />
                <span class="employee-title">已选择员工</span>
                <span class="employee-count">({{ formData.userList?.length || 0 }}人)</span>
              </div>

              <div class="employee-list" v-if="formData.userList && formData.userList.length">
                <div class="employee-tag" v-for="init in formData.userList" :key="init.id">
                  <Icon icon="ep:avatar" class="tag-icon" />
                  <span class="tag-text">{{ init.ownerUsername }}</span>
                </div>
              </div>

              <div class="empty-state" v-else>
                <Icon icon="ep:user" class="empty-icon" />
                <span class="empty-text">暂无选择员工</span>
              </div>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <!-- 弹窗底部 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="submitForm" type="primary" :disabled="formLoading" class="submit-btn">
          <Icon icon="ep:check" class="btn-icon" />
          确 定
        </el-button>
        <el-button @click="dialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取 消
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup>
import { Plus } from '@element-plus/icons-vue'
import * as UserApi from '@/api/system/user'
import { buyUserBill } from '@/api/call/agent/index'

const dialogTitle = ref('购买套餐')
const dialogVisible = ref(false)
const formLoading = ref(false)

const formData = ref({
  tenantId: null,
  count: null,
  productCode: null, //simple-call:点呼； double-call : 双向回拨
  day: null,
  userList: [],
  userIds: []
})
const deptThree = ref(null)
const list = ref([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: 50,
  deptId: '',
  tenantId: null
})
const multipleTable = ref(null)
const formRules = reactive({
  count: [{ required: true, message: '请输入购买分钟', trigger: 'blur' }],
  day: [{ required: true, message: '请输入有效期期', trigger: 'blur' }]
})
const formRef = ref()
const loading = ref(false)
const open = (type, adminId, arr) => {
  dialogVisible.value = true
  resetForm()
  queryParams.tenantId = adminId
  formData.value.tenantId = adminId
  formData.value.productCode = type
  formData.value.userList = arr
  formData.value.userIds = arr.map((item) => item.ownerUserId)
  getList()
}
defineExpose({ open })
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    tenantId: null,
    count: null,
    productCode: null, //simple-call:点呼； double-call : 双向回拨
    day: null,
    userList: [],
    userIds: []
  }
  formRef.value?.resetFields()
}
const rowKey = (row) => {
  return row.id
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPageByAdmin(queryParams)
    list.value = data.list
  } finally {
    loading.value = false
  }
}

const emit = defineEmits(['success'])

const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    for (const key in formData.value) {
      if (key == 'userList') {
        delete formData.value[key]
      }
    }
    await buyUserBill(formData.value)
    dialogVisible.value = false
    ElMessage.success('购买话费成功!')
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  padding: 24px;
  background: white;
  max-height: 60vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    max-height: 50vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

.enhanced-form {
  .form-section {
    margin-bottom: 24px;

    &:last-child {
      margin-bottom: 0;
    }

    @media (max-width: 768px) {
      margin-bottom: 20px;
    }
  }

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid $primary-light;
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;

    @media (max-width: 768px) {
      font-size: 14px;
      margin-bottom: 16px;
      padding-bottom: 8px;
    }
  }

  .section-icon {
    color: $primary-color;
    font-size: 18px;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .form-item {
    margin-bottom: 20px;

    @media (max-width: 768px) {
      margin-bottom: 16px;
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 输入框样式 */
.enhanced-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 12px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

.input-unit {
  color: $text-secondary;
  font-size: 12px;
  font-weight: 500;
  padding: 0 4px;
  background: $primary-light;
  border-radius: 4px;
}

/* 员工选择区域 */
.employee-section {
  border: 1px solid $border-light;
  border-radius: $border-radius-small;
  padding: 16px;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);

  @media (max-width: 768px) {
    padding: 12px;
  }
}

.employee-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    margin-bottom: 10px;
    padding-bottom: 6px;
  }
}

.employee-icon {
  color: $primary-color;
  font-size: 16px;
}

.employee-title {
  font-size: 14px;
  font-weight: 600;
  color: $text-primary;

  @media (max-width: 768px) {
    font-size: 13px;
  }
}

.employee-count {
  font-size: 12px;
  color: $text-secondary;
  background: $primary-light;
  padding: 2px 6px;
  border-radius: 4px;
}

.employee-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;

  @media (max-width: 768px) {
    gap: 6px;
  }
}

.employee-tag {
  display: flex;
  align-items: center;
  gap: 6px;
  background: $primary-color;
  color: white;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    background: darken($primary-color, 10%);
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 82, 204, 0.3);
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
  }
}

.tag-icon {
  font-size: 12px;
}

.tag-text {
  white-space: nowrap;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: $text-secondary;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.empty-icon {
  font-size: 32px;
  margin-bottom: 8px;
  opacity: 0.5;

  @media (max-width: 768px) {
    font-size: 28px;
    margin-bottom: 6px;
  }
}

.empty-text {
  font-size: 14px;
  opacity: 0.7;

  @media (max-width: 768px) {
    font-size: 13px;
  }
}

/* 弹窗底部 */
.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 20px 24px;
  background: #fafbff;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
  }
}

.submit-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

.cancel-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #374151;
  }

  .dialog-footer {
    background: #4b5563;
    border-color: #6b7280;
  }

  .section-title {
    color: #f9fafb;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #d1d5db;
      }
    }
  }

  .enhanced-input {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .input-unit {
    background: rgba(96, 165, 250, 0.1);
    color: #60a5fa;
  }

  .employee-section {
    background: linear-gradient(135deg, #4b5563 0%, #374151 100%);
    border-color: #6b7280;
  }

  .employee-title {
    color: #f9fafb;
  }

  .employee-count {
    background: rgba(96, 165, 250, 0.1);
    color: #60a5fa;
  }

  .employee-tag {
    background: #60a5fa;

    &:hover {
      background: #3b82f6;
    }
  }

  .empty-state {
    color: #9ca3af;
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .form-container {
    padding: 12px;
  }

  .enhanced-form {
    .form-section {
      margin-bottom: 16px;
    }

    .section-title {
      font-size: 13px;
      margin-bottom: 12px;
      padding-bottom: 6px;
    }

    .form-item {
      margin-bottom: 12px;

      :deep(.el-form-item__label) {
        font-size: 13px;
      }
    }
  }

  .dialog-footer {
    padding: 12px 16px;
    gap: 8px;
  }

  .employee-section {
    padding: 10px;
  }

  .employee-header {
    margin-bottom: 8px;
    padding-bottom: 4px;
  }

  .employee-title {
    font-size: 12px;
  }

  .employee-count {
    font-size: 11px;
  }

  .employee-tag {
    padding: 3px 6px;
    font-size: 10px;
  }

  .empty-state {
    padding: 12px;
  }

  .empty-icon {
    font-size: 24px;
    margin-bottom: 4px;
  }

  .empty-text {
    font-size: 12px;
  }
}
</style>
